package com.xhd.serverComm;

import java.util.Hashtable;
import java.util.Vector;

import com.xhd.server.ServerConfig;
import com.xhd.server.XServerId;

public class DataTranCenter {
	 private static final int jm603 = 5;
	  public static final String FLAG_START = "<flag_s>";
	  public static final String FLAG_END = "<flag_e>";
	  public static final String FLAG_TEST = "<test>";
	  public static final String TYPE_DATA = "<flag_data>";
	  public static final String TYPE_REQ = "<flag_req>";
	  public static final String TYPE_CONFIRM = "<flag_comfirm>";
	  public static final String TYPE_REGISTER = "<register>";
	  public static final String DATA_LOGIN = "<login>";
	  public static final String DATA_LOGOUT = "<logout>";
	  public static final String DATA_NEW_EMAIL_COUNT = "<newemailcount>";
	  public static final String DATA_MSG = "<msg>";
	  public static final String DATA_ACTION_TIME = "<actiontime>";
	  public static final String DATA_ONLINE = "<online>";
	  public static final String DATA_REFRESH_SESSION = "<refreshsession>";
	  public static final String DATA_REFRESH_DATAPOOL = "<refreshdatapool>";
	  private static int jm604 = 1;
	  private static Vector jm605 = new Vector();
	  private static Vector jm606 = new Vector();
	  private static Vector jm607 = new Vector();
	  private static Vector jm608 = new Vector();
	  private static XServerId jm84 = XServerId.getLocal();
	  private static boolean jm609 = ServerConfig.ServerCommType == 1;

	  private static boolean jm599(String paramString1, String paramString2)
	  {
	    if ("*".equals(paramString2))
	      return true;
	    if ("%".equals(paramString2))
	    {
	      String str1 = ServerConfig.CompanyId + ".";
	      return (!(paramString1.startsWith(str1)));
	    }
	    if (paramString2.equals(paramString1))
	      return true;
	    int i = paramString2.indexOf(46);
	    if ((i > 0) && (i < paramString2.length()))
	    {
	      String str2 = paramString2.substring(0, i);
	      String str3 = paramString2.substring(i + 1);
	      if ((paramString1.startsWith(str2 + ".")) && (((str3.equals("*")) || (str3.equals("0")) || (str3.equals("3")))))
	        return true;
	    }
	    return false;
	  }

	  private static synchronized DataPack jm600(String paramString, int paramInt, DataPack paramDataPack)
	  {
	    if (paramInt == 1)
	    {
	      if (paramDataPack != null)
	      {
	        if (paramDataPack.Id == 0)
	        {
	          paramDataPack.Id = jm604;
	          jm604 += 1;
	        }
	        if ((paramDataPack.TargetServer == null) || (paramDataPack.TargetServer.length() < 1))
	          paramDataPack.TargetServer = paramString;
	        if ((paramDataPack.SourceServer == null) || (paramDataPack.SourceServer.length() < 1))
	          paramDataPack.SourceServer = jm84.getId();
	        jm605.add(paramDataPack);
	      }
	    }
	    else if (paramInt == 2)
	    {
	      paramDataPack = null;
	      for (int i = 0; i < jm605.size(); ++i)
	      {
	        DataPack localDataPack = (DataPack)jm605.get(i);
	        if ((paramString != null) && (!(jm599(localDataPack.TargetServer, paramString))))
	          continue;
	        jm605.remove(i);
	        paramDataPack = localDataPack;
	        break;
	      }
	    }
	    return paramDataPack;
	  }

	  private static synchronized DataPack jm601(String paramString, int paramInt, DataPack paramDataPack)
	  {
	    if (paramInt == 1)
	    {
	      if ((paramDataPack != null) && (paramDataPack.Type == "<flag_data>") && (paramDataPack.SendCount < 5))
	      {
	        paramDataPack.SendTime = System.currentTimeMillis();
	        paramDataPack.SendCount += 1;
	        jm607.add(paramDataPack);
	      }
	    }
	    else if (paramInt == 2)
	    {
	      for (int i = 0; i < jm607.size(); ++i)
	      {
	        DataPack localDataPack1 = (DataPack)jm607.get(i);
	        if (localDataPack1.Id != paramDataPack.Id)
	          continue;
	        jm607.remove(i);
	       
	      }
	    }
	    else if (paramInt == 3)
	    {
	      paramDataPack = null;
	      long l = System.currentTimeMillis();
	      for (int j = 0; j < jm607.size(); ++j)
	      {
	        DataPack localDataPack2 = (DataPack)jm607.get(j);
	        if ((!(jm599(localDataPack2.TargetServer, paramString))) || (l - localDataPack2.SendTime < 300000L))
	          continue;
	        jm607.remove(j);
	        paramDataPack = localDataPack2;
	        break;
	      }
	    }
	    label200: return paramDataPack;
	  }

	  private static synchronized DataPack jm602(int paramInt, DataPack paramDataPack)
	  {
	    if (paramInt == 1)
	      if (paramDataPack != null)
	      {
	        if (paramDataPack.Id < 1)
	        {
	          paramDataPack.Id = jm604;
	          jm604 += 1;
	        }
	        jm606.add(paramDataPack);
	      }
	    else if (paramInt == 2)
	      if (jm606.size() > 0)
	        paramDataPack = (DataPack)jm606.remove(0);
	      else
	        paramDataPack = null;
	    return paramDataPack;
	  }

	  protected static DataPack getSendData(String paramString)
	  {
	    DataPack localDataPack = jm600(paramString, 2, null);
	    if (localDataPack == null)
	      localDataPack = jm601(paramString, 3, null);
	    return localDataPack;
	  }

	  protected static DataPack getSendData()
	  {
	    DataPack localDataPack = jm600(null, 2, null);
	    if (localDataPack == null)
	      localDataPack = jm601(null, 3, null);
	    return localDataPack;
	  }

	  protected static void toConfirm(DataPack paramDataPack)
	  {
	    jm601(paramDataPack.TargetServer, 1, paramDataPack);
	  }

	  protected static void onSendFailed(DataPack paramDataPack)
	  {
	    jm600(paramDataPack.TargetServer, 1, paramDataPack);
	  }

	  protected static void onReceive(DataPack paramDataPack)
	  {
	    if (paramDataPack == null)
	      return;
	    String str = paramDataPack.TargetServer;
	    if (str == null)
	      str = jm84.getId();
	    if (ServerConfig.ServerCommType == 1)
	    {
	      if ((ServerConfig.ServerType != 3) && (str.startsWith(ServerConfig.CompanyId + ".")))
	      {
	        DataPack localDataPack;
	        if ("<flag_comfirm>".equals(paramDataPack.Type))
	        {
	          localDataPack = new DataPack();
	          localDataPack.Id = Integer.parseInt(paramDataPack.DataType);
	          localDataPack.TargetServer = paramDataPack.SourceServer;
	          jm601(localDataPack.TargetServer, 2, localDataPack);
	        }
	        else
	        {
	          jm602(1, paramDataPack);
	          if (!("<flag_data>".equals(paramDataPack.Type)))
	            return;
	          localDataPack = new DataPack();
	          localDataPack.Type = "<flag_comfirm>";
	          localDataPack.DataType = String.valueOf(paramDataPack.Id);
	          localDataPack.TargetServer = paramDataPack.SourceServer;
	          localDataPack.SourceServer = paramDataPack.TargetServer;
	          jm600(localDataPack.TargetServer, 1, localDataPack);
	        }
	      }
	      else
	      {
	        jm600(paramDataPack.TargetServer, 1, paramDataPack);
	      }
	    }
	    else
	    {
	      if ("<flag_comfirm>".equals(paramDataPack.Type))
	        return;
	      jm602(1, paramDataPack);
	    }
	  }

	  protected static DataPack getReceiveData()
	  {
	    return jm602(2, null);
	  }

	  public static void addSendReq(String paramString1, String paramString2, Vector paramVector)
	  {
	    Vector localVector = getServers(paramString1);
	    if ((localVector == null) || (paramString2 == null))
	      return;
	    for (int i = 0; i < localVector.size(); ++i)
	    {
	      DataPack localDataPack = new DataPack();
	      localDataPack.Type = "<flag_req>";
	      localDataPack.TargetServer = ((String)localVector.get(i));
	      localDataPack.DataType = paramString2;
	      if (paramString2.charAt(0) != '<')
	        localDataPack.DataType = "<" + localDataPack.DataType + ">";
	      if (paramVector != null)
	        localDataPack.Data = paramVector;
	      jm600(paramString1, 1, localDataPack);
	    }
	  }

	  public static void addSendData(String paramString1, String paramString2, Object paramObject)
	  {
	    Vector localVector1 = getServers(paramString1);
	    if ((localVector1 == null) || (paramString2 == null) || (paramObject == null))
	      return;
	    for (int i = 0; i < localVector1.size(); ++i)
	    {
	      DataPack localDataPack = new DataPack();
	      localDataPack.Type = "<flag_data>";
	      localDataPack.TargetServer = ((String)localVector1.get(i));
	      Vector localVector2 = new Vector();
	      localVector2.addElement(paramObject);
	      localDataPack.Data = localVector2;
	      if (paramString2.charAt(0) != '<')
	        localDataPack.DataType = "<" + paramString2 + ">";
	      else
	        localDataPack.DataType = paramString2;
	      jm600(paramString1, 1, localDataPack);
	    }
	  }

	  public static void addSendData(String paramString1, String paramString2, Vector paramVector)
	  {
	    Vector localVector = getServers(paramString1);
	    if ((localVector == null) || (paramString2 == null) || (paramVector == null))
	      return;
	    for (int i = 0; i < localVector.size(); ++i)
	    {
	      DataPack localDataPack = new DataPack();
	      localDataPack.Type = "<flag_data>";
	      localDataPack.TargetServer = ((String)localVector.get(i));
	      if (paramString2.charAt(0) != '<')
	        localDataPack.DataType = "<" + paramString2 + ">";
	      else
	        localDataPack.DataType = paramString2;
	      localDataPack.Data = paramVector;
	      jm600(paramString1, 1, localDataPack);
	    }
	  }

	  public static void addSendData(String paramString1, String paramString2, Object paramObject1, Object paramObject2)
	  {
	    Vector localVector1 = getServers(paramString1);
	    if ((localVector1 == null) || (paramString2 == null) || ((paramObject1 == null) && (paramObject2 == null)))
	      return;
	    for (int i = 0; i < localVector1.size(); ++i)
	    {
	      DataPack localDataPack = new DataPack();
	      localDataPack.Type = "<flag_data>";
	      localDataPack.TargetServer = ((String)localVector1.get(i));
	      Vector localVector2 = new Vector();
	      if (paramObject1 != null)
	        localVector2.addElement(paramObject1);
	      if (paramObject2 != null)
	        localVector2.addElement(paramObject2);
	      localDataPack.Data = localVector2;
	      if (paramString2.charAt(0) != '<')
	        localDataPack.DataType = "<" + paramString2 + ">";
	      else
	        localDataPack.DataType = paramString2;
	      localDataPack.TargetServer = paramString1;
	      jm600(paramString1, 1, localDataPack);
	    }
	  }

	  protected static void registerServer(String paramString)
	  {
	    XServerId localXServerId = null;
	    for (int i = 0; i < jm608.size(); ++i)
	    {
	      localXServerId = (XServerId)jm608.get(i);
	      if (paramString.equals(localXServerId.getId()))
	        break;
	    }
	    localXServerId = new XServerId(paramString);
	    jm608.add(localXServerId);
	  }

	  protected static Vector getServers(String paramString)
	  {
	    if ((paramString == null) || (paramString.length() < 1))
	      return null;
	    Vector localVector = null;
	    if (jm609)
	    {
	      localVector = new Vector();
	      XServerId localXServerId1 = new XServerId(paramString);
	      for (int i = 0; i < jm608.size(); ++i)
	      {
	        XServerId localXServerId2 = (XServerId)jm608.get(i);
	        if (paramString.equals(localXServerId2.getId()))
	        {
	          localVector.add(paramString);
	          
	        }
	        if (localXServerId1.getServerType() == 0)
	        {
	          if (!(localXServerId1.getCompId().equals(localXServerId2.getCompId())))
	            continue;
	          localVector.add(localXServerId2.getId());
	        }
	        else
	        {
	          if ((localXServerId1.getServerId() != null) || (!(localXServerId1.getCompId().equals(localXServerId2.getCompId()))) || (localXServerId1.getServerType() != localXServerId2.getServerType()))
	            continue;
	          localVector.add(localXServerId2.getId());
	        }
	      }
	    }
	    else
	    {
	      localVector = new Vector(1);
	      localVector.add(paramString);
	    }
	    label187: return localVector;
	  }

	  public static Vector getQuery(int paramInt)
	  {
	    Vector localVector = null;
	    if (paramInt == 1)
	      localVector = (Vector)jm605.clone();
	    else if (paramInt == 2)
	      localVector = (Vector)jm606.clone();
	    else if (paramInt == 3)
	      localVector = (Vector)jm607.clone();
	    return localVector;
	  }

	  public static boolean start()
	  {
	    if (ServerConfig.ServerCommType == 0)
	      return true;
	    Object localObject2;
	    Object localObject3;
	    Object localObject1;
	    if ((ServerConfig.ServerCommType == 1) && (ServerConfig.ServerComm.size() > 0))
	    {
	      for (int i = 0; i < ServerConfig.ServerComm.size(); ++i)
	      {
	        localObject2 = (Hashtable)ServerConfig.ServerComm.get(i);
	        localObject3 = (String)((Hashtable)localObject2).get("comm-ip");
	        String str = (String)((Hashtable)localObject2).get("target");
	        boolean bool = "true".equalsIgnoreCase((String)((Hashtable)localObject2).get("online"));
	        int j = Integer.parseInt((String)((Hashtable)localObject2).get("comm-port"));
	        Object localObject4;
	        if ("true".equalsIgnoreCase((String)((Hashtable)localObject2).get("is-server")))
	        {
	          localObject4 = new CommServerThread(j);
	          ((CommServerThread)localObject4).start();
	        }
	        else
	        {
	          localObject4 = new CommClientThread((String)localObject3, j);
	          ((CommClientThread)localObject4).setConf(str, bool);
	          ((CommClientThread)localObject4).start();
	        }
	      }
	      localObject1 = new DataProcessThread();
	      ((DataProcessThread)localObject1).start();
	    }
	    else if (ServerConfig.ServerCommType == 2)
	    {
	      localObject1 = new Hashtable(3);
	      ((Hashtable)localObject1).put("jmsprovider", ServerConfig.JmsProvider);
	      ((Hashtable)localObject1).put("jmsfactory", ServerConfig.JmsFactory);
	      ((Hashtable)localObject1).put("jmsurl", ServerConfig.JmsUrl);
	      ((Hashtable)localObject1).put("serverid", XServerId.getLocal().getId());
	      localObject2 = new CPCJmsCommThread((Hashtable)localObject1);
	      ((CPCJmsCommThread)localObject2).start();
	      localObject3 = new DataProcessThread();
	      ((DataProcessThread)localObject3).start();
	    }
	    return true;
	  }
}
